import java.net.ServerSocket;
import java.net.Socket;
import java.io.*;
import java.net.*;
import java.lang.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;

public class writeToFileClass {
 
  int client_id;
    Socket socket;    
    String folder;
    String vehi;
    FileOutputStream fout;
    String tempTxt=null;
    StringBuilder line=new StringBuilder();
    
    GeoFun geofun = new GeoFun();
    sendAlert sa = new sendAlert();
  
  public void makeFolder(String strFilename,String dataTxt,String vehino) throws Exception
  {
   File fos=new File(strFilename);
   FileWriter fw=new FileWriter(fos,true);
   BufferedWriter bw=new BufferedWriter(fw);
   bw.write(dataTxt);
   bw.close();
  }
   public boolean readFile(String path,String Text,String vehi1) 
   {
    String str1=null;
    try
    {
     FileInputStream fstream = new FileInputStream(path);
     DataInputStream in = new DataInputStream(fstream);
     BufferedReader br = new BufferedReader(new InputStreamReader(in)); 
     str1= br.readLine();
     in.close();
    }
    catch (Exception e){
     //System.err.println("Error: " + e.getMessage());
     str1="";
    }
    if(str1.indexOf(Text)==-1)
    {
     return true;
    }
    else
    {
     return false;
    }
    }
    
   public void readData(String strText) throws Exception
   {
    String[] newDateTime;
    String fTxt = null;
    String subFolder=null;
    String strLine=null;
    try
    {
      
      strText = strText.replace("$$","");
      
      String[] data1=strText.split(",");
      //System.out.println("Data "+strText+" "+data1[1]); 
      String[] datetime = data1[6].split("(?<=\\G.{2})");
      
      vehi = data1[1];       
      
      newDateTime=geofun.makeDateTime(datetime[2],datetime[1],datetime[0],datetime[3],datetime[4],datetime[5]).split(",");  
          

      String latTxt = data1[4].replace(".","");     
      String lngTxt = data1[5].replace(".","");
      
      String otherParam = "";
      
      String noOfSatelite = data1[8];
      
      String GSMSignal = data1[9];
      otherParam += "[GSMSignal="+GSMSignal+"] ";
      
      String speed = data1[10];
      String heading = data1[11];
      
      String HDOP = data1[12];
      otherParam += "[HDOP="+HDOP+"] ";
      
      String Altitute = data1[13];
      
      Long Milege = Long.parseLong(data1[14]);
      otherParam += "[Mileage="+(Milege / 1000)+"] ";
      
      Long totalSecs = Long.parseLong(data1[15]);
      String timeString = geofun.secondsToString(totalSecs);

      
      otherParam += "[Runtime="+timeString+"] ";
      
      String[] BaseID = data1[16].split("\\|");
      String MCC = BaseID[0];
      String MNC = BaseID[1];
      Long LAC = Long.parseLong(BaseID[2],16);
      Long CI = Long.parseLong(BaseID[3],16);
      
      otherParam += "[MCC="+MCC+"] ";
      otherParam += "[MNC="+MNC+"] ";
      otherParam += "[LAC="+LAC+"] ";
      otherParam += "[CI="+CI+"] ";
      
      String IOState1 =  geofun.HexToBinary(data1[17]);   
      String[] IOState = IOState1.split("(?<=\\G.{1})");   
     
      for(int i=0; i < IOState.length; i++)
      {
         otherParam += "["+(i+1)+"="+IOState[i]+"] ";
      }
      
      String[] AD = data1[18].split("\\|");
      for(int i=0; i < AD.length; i++)
      {
         otherParam += "[10"+(i+1)+"="+Long.parseLong(AD[i],16)+"] ";
      }
      
      if(data1[19].length() > 0)
      {
        otherParam += "[RFID="+Long.parseLong(data1[19],16)+"] ";
      }
      else
      {
        otherParam += "[RFID=0] ";
      }
      
      if(data1[20].length() > 0)
      {
        otherParam += "[PIC_NAME="+data1[20]+"] ";
      }
      else
      {
        otherParam += "[PIC_NAME=0] ";
      }

      /*if(data1[21].length() > 0)
      {
        otherParam += "[FENCE_NO="+data1[21]+"] ";
      }
      else
      {
        otherParam += "[FENCE_NO=0] ";
      }*/

      String CHKSUM = data1[data1.length-1].replace("*","");
      CHKSUM = ""+Long.parseLong(CHKSUM,16);
      
      
      fTxt="@"+vehi+","+lngTxt+","+latTxt+","+speed+","+heading+","+Altitute+","+noOfSatelite+","+otherParam+","+newDateTime[0]+","+newDateTime[1]+"#";   
      //System.out.println("ss"+fTxt+"\n");      
      
      //subFolder="E:/xampp/htdocs/src/data/"+newDateTime[0]+"/";
      subFolder="/opt/lampp/htdocs/src/data/"+newDateTime[0]+"/";
      //System.out.println("subFolder "+subFolder);
      File sub = new File(subFolder);
      try
      {
        if(sub.mkdirs()){}
        else{}
      }
      catch(Exception e)
      {
        e.printStackTrace();
      }
      subFolder+=vehi+".txt";
      //System.out.println(subFolder);      
      
      if(readFile(subFolder,fTxt,vehi))
      {
        //System.out.println("Data "+fTxt);
        
        sa.sendSMSAlert(fTxt);
        makeFolder(subFolder,fTxt,vehi);
      } 
      

    }   
    catch(Exception e)
    {
       //System.out.println("Problem finding file");
    }
  }
 

 /*public static void main (String[] args) 
 {
   writeToFileClass wtf= new writeToFileClass();
   try
   {
    //wtf.readData("$CHD5991,27,07,09,10,10,01,17266264N,078260380E,00.0,164#$CHD5991,27,07,09,10,10,21,17266264N,078260380E,00.0,178#");
    wtf.readData("$$T131,011691003092726,AAA,35,17.445800,78.464000,111122115946,A,9,29,0,0,0,535,271786,259028,404|49|13|399,0000,39|35|37|93A|3CD,,,*7B");
   }
   catch(Exception e)
   {
    
   }
 }*/
    
}
